package net.dgg.rz.common.utils;

import net.dgg.iboss.base.util.DesUtil;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.service.CommonMangerExtService;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @className SearchUtils
 * @Author Quanlin Wan
 * @Description //TODO
 * @Date 2018/12/24 9:18
 **/
@Component
public class SearchUtils {
    @Autowired
    private DesUtil desUtil;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private UserService userService;
    @Autowired
    private DataPermissionAssocService dataPermissionAssocService;
    /**
     * @Author Quanlin Wan
     * @Description //TODO 搜索条件参数注入
     *
     * @Date 9:22 2018/12/24
     * @Param orderTime 下单时间  allotDate 分配时间  suspendTime 暂缓时间 customerPhone 客户电话（这里进行加密）userID当前用户id(PC端不需要)
     * @return 
     **/
    
    public  void paramsOperate(Map params) {

        UserEntity userEntity = this.getcurrUser(params);

        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "未登录，请登录后再试");
        //时间超期状态
        List<String> statusAttrExt = new ArrayList<>();
        if (StringUtils.isNotEmpty(String.valueOf(params.get("SQSTATUS")))) {
            switch (String.valueOf(params.get("SQSTATUS"))) {
                   //未超期
                case "OUT_TIME_STATE1":
                    params.put("TIME_OUT_STATUS", ScProductConstant.TIME_OUT);
                    break;
                    //分配超期
                case "OUT_TIME_STATE2":
                    params.put("TIME_OUT_STATUS", ScProductConstant.TIME_OUT);
                    statusAttrExt.add(ScProductConstant.RZSCORDER_STATUS14);
                    params.put("statusAttrExt",statusAttrExt);
                    break;
                    //接收超期
                case "OUT_TIME_STATE3":
                    params.put("TIME_OUT_STATUS", ScProductConstant.TIME_OUT);
                    statusAttrExt.add(ScProductConstant.RZSCORDER_STATUS11);
                    statusAttrExt.add(ScProductConstant.RZSCORDER_STATUS01);
                    statusAttrExt.add(ScProductConstant.RZSCORDER_STATUS16);
                    params.put("statusAttrExt",statusAttrExt);
                    break;
                    //节点超期
                case "OUT_TIME_STATE4":
                    params.put("TIME_OUT_STATUS", ScProductConstant.TIME_OUT);
                    break;
                    //订单超期
                case "OUT_TIME_STATE5":
                    params.put("TIME_OUT_STATUS", ScProductConstant.TIME_OUT);
                    break;
                default:
                    break;
            }

        }
        //排序
        params.put("orderDir", "asc");
        params.put("orderColumn", "t.update_time");
        //电话号码
        if(StringUtils.isNotEmpty(String.valueOf(params.get("searchKey")))){
            params.put("searchKeyDes", desUtil.encrypt(String.valueOf(params.get("searchKey"))));
        }

        params.put("flag", 0);
       //下单时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("orderTime")))){
            String orderTime=String.valueOf(params.get("orderTime"));
            String signTimeStart=orderTime.substring(0,orderTime.indexOf("~"));
            signTimeStart= signTimeStart.trim();
            String signTimeEnd = orderTime.substring(orderTime.indexOf("~") + 1);
            signTimeEnd= signTimeEnd.trim();
            params.put("signTimeStart", signTimeStart);
            params.put("signTimeEnd", signTimeEnd);
        }
       //下单时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("signTime")))){
            String orderTime=String.valueOf(params.get("signTime"));
            String signTimeStart=orderTime.substring(0,orderTime.indexOf("~"));
            signTimeStart= signTimeStart.trim();
            String signTimeEnd = orderTime.substring(orderTime.indexOf("~") + 1);
            signTimeEnd= signTimeEnd.trim();
            signTimeEnd= signTimeEnd.trim();
            params.put("signTimeStart", signTimeStart);
            params.put("signTimeEnd", signTimeEnd);
        }

           // 分配时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("allotDate")))){
            String allotDate=String.valueOf(params.get("allotDate"));
            String allotDatetart=allotDate.substring(0,allotDate.indexOf("~"));
            allotDatetart= allotDatetart.trim();
            String allotDateEnd = allotDate.substring(allotDate.indexOf("~") + 1);
            allotDateEnd= allotDateEnd.trim();
            params.put("allotDateStart", allotDatetart);
            params.put("allotDateEnd", allotDateEnd);
        }
        //客户电话 1.0.3迭代取消该搜索条件，改为客户名称
    /*    if(StringUtils.isNotEmpty(String.valueOf(params.get("customerPhone")))){
            String customerPhone=String.valueOf(params.get("customerPhone"));
             customerPhone=desUtil.encrypt(customerPhone);
            params.put("customerPhone", customerPhone);
        }*/

        //暂缓时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("suspendTime")))){
            String suspendTime=String.valueOf(params.get("suspendTime"));
            String suspendTimeStart=suspendTime.substring(0,suspendTime.indexOf("~"));
            suspendTimeStart= suspendTimeStart.trim();
            String suspendTimeEnd = suspendTime.substring(suspendTime.indexOf("~") + 1);
            suspendTimeEnd= suspendTimeEnd.trim();
            params.put("suspendTimeStart", suspendTimeStart);
            params.put("suspendTimeEnd", suspendTimeEnd);
        }
        if(StringUtils.isNotEmpty(String.valueOf(params.get("abandonReasonTime")))){
            String abandonReasonTime=String.valueOf(params.get("abandonReasonTime"));
            String abandonReasonTimeStart=abandonReasonTime.substring(0,abandonReasonTime.indexOf("~"));
            abandonReasonTimeStart= abandonReasonTimeStart.trim();
            String abandonReasonTimeEnd = abandonReasonTime.substring(abandonReasonTime.indexOf("~") + 1);
            abandonReasonTimeEnd= abandonReasonTimeEnd.trim();
            params.put("abandonReasonTimeStart", abandonReasonTimeStart);
            params.put("abandonReasonTimeEnd", abandonReasonTimeEnd);
        }
        //放款时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("loanTime")))){
            String abandonReasonTime=String.valueOf(params.get("loanTime"));
            String abandonReasonTimeStart=abandonReasonTime.substring(0,abandonReasonTime.indexOf("~"));
            abandonReasonTimeStart= abandonReasonTimeStart.trim();
            String abandonReasonTimeEnd = abandonReasonTime.substring(abandonReasonTime.indexOf("~") + 1);
            abandonReasonTimeEnd= abandonReasonTimeEnd.trim();
            params.put("loanTimeStart", abandonReasonTimeStart);
            params.put("loanTimeEnd", abandonReasonTimeEnd);
        }

        //完成时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("completedTime")))){
            String abandonReasonTime=String.valueOf(params.get("completedTime"));
            String abandonReasonTimeStart=abandonReasonTime.substring(0,abandonReasonTime.indexOf("~"));
            abandonReasonTimeStart= abandonReasonTimeStart.trim();
            String abandonReasonTimeEnd = abandonReasonTime.substring(abandonReasonTime.indexOf("~") + 1);
            abandonReasonTimeEnd= abandonReasonTimeEnd.trim();
            params.put("completedTimeStart", abandonReasonTimeStart);
            params.put("completedTimeEnd", abandonReasonTimeEnd);
        }

        //接单时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("receiveOrderDate")))){
            String abandonReasonTime=String.valueOf(params.get("receiveOrderDate"));
            String abandonReasonTimeStart=abandonReasonTime.substring(0,abandonReasonTime.indexOf("~"));
            abandonReasonTimeStart= abandonReasonTimeStart.trim();
            String abandonReasonTimeEnd = abandonReasonTime.substring(abandonReasonTime.indexOf("~") + 1);
            abandonReasonTimeEnd= abandonReasonTimeEnd.trim();
            params.put("receiveOrderDateStart", abandonReasonTimeStart);
            params.put("receiveOrderDateEnd", abandonReasonTimeEnd);
        }
        //最新备注时间，复用于驳回时间等
        if(StringUtils.isNotEmpty(String.valueOf(params.get("lastRemarkTime")))){
            String lastRemarkTime=String.valueOf(params.get("lastRemarkTime"));
            String lastRemarkTimeStart=lastRemarkTime.substring(0,lastRemarkTime.indexOf("~"));
            lastRemarkTimeStart= lastRemarkTimeStart.trim();
            String lastRemarkTimeEnd = lastRemarkTime.substring(lastRemarkTime.indexOf("~") + 1);
            lastRemarkTimeEnd= lastRemarkTimeEnd.trim();
            params.put("lastRemarkTimeStart", lastRemarkTimeStart);
            params.put("lastRemarkTimeEnd", lastRemarkTimeEnd);
        }


        if (StringUtils.isNotEmpty(String.valueOf(params.get("audit")))) {
            String audit = params.get("audit").toString();
            switch (audit) {
                case "selfAuditor":
                    params.put("auditorId", userEntity.getId());
                    break;
                case "otherAuditor":
                    params.put("otherAuditor", userEntity.getId());
                    break;
                default:
                    break;
            }
        }
        //查询部门及以下
        List<Long> signOrgOrBusIdList = null;
        if(net.dgg.rz.common.utils.StringUtils.isNotEmpty(params.get("signOrgOrBusId"))){
            signOrgOrBusIdList = commonMangerExtService.getChildOrganizationList(Long.parseLong(params.get("signOrgOrBusId")+""));
        }
        params.put("signOrgOrBusIdList",signOrgOrBusIdList);
        //下面参数暂时在报表,已退单，废单中使用
        List<Long> flowUserDeps = null;
        if (StringUtils.isNotEmpty(String.valueOf(params.get("flowUserDep")))) {
            flowUserDeps = commonMangerExtService.getChildOrganizationList(Long.parseLong(params.get("flowUserDep")+""));
        }
        params.put("flowUserDeps", flowUserDeps);

    }


    /**
     * @Author Quanlin Wan
     * @Description //TODO APP列表参数处理
     * @Date 16:04 2019/4/11
     * @Param
     * @return
     **/
    public  void paramsOperateApp(Map params) {

        UserEntity userEntity = this.getcurrUser(params);
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "未登录，请登录后再试");

        //排序
        params.put("orderDir", "desc");
        params.put("orderColumn", "t.update_time");

        //电话号码加密搜索
        if(StringUtils.isNotEmpty(String.valueOf(params.get("searchKey")))){
            params.put("searchKeyDes", desUtil.encrypt(String.valueOf(params.get("searchKey"))));
        }
        params.put("flag", 0);

        //分页
        if(StringUtils.isNotEmpty(String.valueOf(params.get("size")))&&StringUtils.isNotEmpty(String.valueOf(params.get("pageSize")))){
            Long size=Long.valueOf(String.valueOf(params.get("size")));
            Long pageSize=Long.valueOf(String.valueOf(params.get("pageSize")));
            params.put("start", (size - 1) * pageSize);
            params.put("length", pageSize);
        }else{
            params.put("size", 1);
            params.put("pageSize", 10);
            params.put("start", 0);
            params.put("length", 10);
        }

    }


    public  void paramsRoyalty(Map params) {
        //是推送
        params.put("isPush", 1);
        //是可用
        params.put("isValid", 1);
        //时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("royaltyTime")))){
            String royaltyTime=String.valueOf(params.get("royaltyTime"));
            String royaltyTimeStart=royaltyTime.substring(0,royaltyTime.indexOf("~"));
            royaltyTimeStart= royaltyTimeStart.trim();
            String royaltyTimeEnd = royaltyTime.substring(royaltyTime.indexOf("~") + 1);
            royaltyTimeEnd= royaltyTimeEnd.trim();
            params.put("royaltyTimeStart", royaltyTimeStart);
            params.put("royaltyTimeEnd", royaltyTimeEnd);
        }
    }
    /**
     * 获取当前月第一天到当前时间的字符串
     */
    public static String getMonthDay(){
        SimpleDateFormat sdfFis = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        String firstDay = sdfFis.format(new Date())+"-01";
        String curDay = sdfDay.format(new Date());
        return firstDay+" ~ "+curDay;
    }
    
    /**
     * @Author Quanlin Wan
     * @Description //TODO 当前登录用户数据权限
     * @Date 11:01 2019/4/11
     * @Param 
     * @return 
     **/
    public DataAuthDTO getcurrUserAuth(Map params, String code) {
        DataAuthDTO dataAuth = new DataAuthDTO();
        if(StringUtils.isNotEmpty(params.get("userLoginName"))){
//            Long userId= Long.valueOf(String.valueOf(params.get("userId")));
            String loginName=String.valueOf(params.get("userLoginName"));
            UserEntity userEntity = userService.findUserByLoginName(loginName);
            dataAuth = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds(code, userEntity.getId());
        }else {
            dataAuth=dataPermissionAssocService.selectAuthToConfigureRturnOrgIds(code);
        }
        return dataAuth;
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 获取当前用户
     * @Date 11:05 2019/4/11
     * @Param
     * @return
     **/
    public UserEntity getcurrUser(Map params) {
        UserEntity userEntity = new UserEntity();
        if(StringUtils.isNotEmpty(params.get("userLoginName"))){
            String loginName=String.valueOf(params.get("userLoginName"));
            userEntity = userService.findUserByLoginName(loginName);
        }else if(this.sessionManager.exsitSession()){
            userEntity =  this.sessionManager.getCurrentUser();
        }else {
            userEntity = null;
        }
        ValidateUtils.strNotEmpty(userEntity, ProductOrderExcption.class, "当前用户未登录");
        return userEntity;
    }

    /**
     * 成单客户组装数据
     * @param params
     */
    public  void paramsComplete(Map params) {
        //时间
        if(StringUtils.isNotEmpty(String.valueOf(params.get("loanTime")))){
            String loanTime=String.valueOf(params.get("loanTime"));
            String loanTimeStart=loanTime.substring(0,loanTime.indexOf("~"));
            loanTimeStart= loanTimeStart.trim();
            String loanTimeEnd = loanTime.substring(loanTime.indexOf("~") + 1);
            loanTimeEnd= loanTimeEnd.trim();
            params.put("loanTimeStart", loanTimeStart);
            params.put("loanTimeEnd", loanTimeEnd);
        }
        if(StringUtils.isNotEmpty(String.valueOf(params.get("repaymentDate")))){
            String repaymentDate=String.valueOf(params.get("repaymentDate"));
            String repaymentDateStart=repaymentDate.substring(0,repaymentDate.indexOf("~"));
            repaymentDateStart= repaymentDateStart.trim();
            String repaymentDateEnd = repaymentDate.substring(repaymentDate.indexOf("~") + 1);
            repaymentDateEnd= repaymentDateEnd.trim();
            params.put("repaymentDateStart", repaymentDateStart);
            params.put("repaymentDateEnd", repaymentDateEnd);
        }
    }
    
}
